﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common;
using System.Reflection;

namespace net.veekee.common.data
{
    [Serializable]
    public class CollectionManager<K>
    {
        private Dictionary<K, Object> items = null;
        public int ItemCount
        {
            get
            {
                return this.items.Count;
            }
        }
        public Dictionary<K, Object>.KeyCollection Keys
        {
            get
            {
                return this.items.Keys;
            }
        }

        public CollectionManager()
        {
            this.items = new Dictionary<K, Object>();
        }

        public virtual void Set<T>(K name, T value)
        {
            Log.GetInstance().Trace("Setting field {0} with {1}", name, value);
            if (this.items.ContainsKey(name))
                this.items[name] = value;
            else
                this.items.Add(name, value);
        }

        public virtual T Get<T>(K name, T defaultValue)
        {
            try
            {
                if (this.items.ContainsKey(name))
                {
                    if (this.items[name] != null)
                    {
                        if (typeof(T).IsAssignableFrom(this.items[name].GetType()))
                            return (T)this.items[name];
                    }
                }
            }
            catch
            {
            }
            return defaultValue;
        }

        public T Get<T>(K name)
        {
            return this.Get<T>(name, default(T));
        }

        public String Get(K name, String defaultValue)
        {
            return this.Get<String>(name, defaultValue);
        }

        public String Get(K name)
        {
            return this.Get<String>(name, "");
        }

        public void Clear()
        {
            this.items.Clear();
        }

        public int GetAsInt(K name, int defaultValue)
        {
            String val = this.Get<String>(name, defaultValue.ToString());
            Int32.TryParse(val, out defaultValue);
            return defaultValue;
        }

        public int GetAsInt(K name)
        {
            return this.GetAsInt(name, 0);
        }
        
        public List<Object> Dump()
        {
            List<Object> list = new List<Object>();
            foreach (KeyValuePair<K, Object> kvp in this.items)
            {
                list.Add(kvp.Value.ToString());
            }
            return list;
        }

        public void DumpToLog()
        {
            foreach (KeyValuePair<K, Object> kvp in this.items)
            {
                Log.GetInstance().Debug("{0} => '{1}'", kvp.Key, kvp.Value);
            }
        }

        public override String ToString()
        {
            return String.Format("{0} fields", this.ItemCount);
        }
    }
}
